using System;
using System.Collections;
using UnityEngine;

namespace PKGames.MiniGames.VoxelGame.UI.GameUI.Scripts.GamePanel
{
	[Serializable]
	public class ListScrollAnimation
	{
		[SerializeField]
		private RectTransform _viewPortRectTransform;

		[SerializeField]
		private RectTransform _contentRectTransform;

		private const float AnimationTime = 1.5f;

		public IEnumerator Setup()
		{
			yield return null;
			Vector2 size = _contentRectTransform.rect.size;
			float contentSize = size.x;
			Vector2 size2 = _viewPortRectTransform.rect.size;
			float viewPortSize = size2.x;
			Vector2 startOffsetMax = new Vector2(viewPortSize, 0f);
			Vector2 startOffsetMin = new Vector2(viewPortSize - contentSize, 0f);
			_contentRectTransform.offsetMax = startOffsetMax;
			_contentRectTransform.offsetMin = startOffsetMin;
			yield return null;
		}

		public IEnumerator AnimateListIfPossible()
		{
			Vector2 size = _contentRectTransform.rect.size;
			float contentSize = size.x;
			Vector2 size2 = _viewPortRectTransform.rect.size;
			float viewPortSize = size2.x;
			if (contentSize > viewPortSize)
			{
				yield return PlayScrollListLeftAnimation();
			}
		}

		private IEnumerator PlayScrollListLeftAnimation()
		{
			Vector2 size = _contentRectTransform.rect.size;
			float contentSize = size.x;
			Vector2 size2 = _viewPortRectTransform.rect.size;
			float viewPortSize = size2.x;
			Vector2 startOffsetMax = new Vector2(viewPortSize, 0f);
			Vector2 endOffsetMax = new Vector2(contentSize, 0f);
			Vector2 startOffsetMin = new Vector2(viewPortSize - contentSize, 0f);
			Vector2 endOffsetMin = new Vector2(0f, 0f);
			AnimationCurve animationCurve = AnimationCurve.EaseInOut(0f, 0f, 1.5f, 1f);
			for (float time = 0f; time < 1.5f; time += Time.deltaTime)
			{
				_contentRectTransform.offsetMax = Vector2.Lerp(startOffsetMax, endOffsetMax, animationCurve.Evaluate(time));
				_contentRectTransform.offsetMin = Vector2.Lerp(startOffsetMin, endOffsetMin, animationCurve.Evaluate(time));
				yield return null;
			}
			_contentRectTransform.offsetMax = endOffsetMax;
			_contentRectTransform.offsetMin = endOffsetMin;
		}
	}
}
